const { DataTypes } = require('sequelize');

// const sequelize = require("@/db/seq");
const sequelize = require("@/db/seq");

const Menus = sequelize.define("blog_menus", {
  title: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '菜单标题'
  },
  path: {
    type: DataTypes.STRING,
    comment: '菜单路径'
  },
  name: {
    type: DataTypes.STRING,
    comment: '菜单名称'
  },
  redirect: {
    type: DataTypes.STRING,
    comment: '重定向'
  },
  component: {
    type: DataTypes.STRING,
    comment: '菜单组件'
  },
  icon: {
    type: DataTypes.STRING,
    comment: '菜单图标'
  },
  parent_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    references: {
      model: 'blog_menus',
      key: 'id'
    },
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE',
    comment: '父级菜单ID'
  },
  status: {
    type: DataTypes.TINYINT,
    allowNull: false,
    defaultValue: 1,
    comment: '菜单状态 1: 启用 0: 禁用'
  },
  order: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    comment: '排序'
  },
  type: {
    type: DataTypes.TINYINT,
    allowNull: false,
    defaultValue: 1,
    // comment: '菜单类型 1: 目录 2: 菜单 3: 按钮'
    comment: '菜单类型 1: 菜单 2: 按钮'
  },
  permission_code: {
    type: DataTypes.STRING,
    comment: '权限码'
  },
}, {
  freezeTableName: true, // 禁用修改表名; 默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数.
  // 如果不启用,则表名称默认与模型名称相同.
})

Menus.associate = (models) => {
  Menus.belongsToMany(models.blog_roles, {
    through: models.role_menus,
    foreignKey: 'menu_id',
    otherKey: 'role_id',
    as: 'roles'
  });
  Menus.hasMany(models.blog_menus, {
    foreignKey: 'parent_id',
    as: 'children'
  });
};
// Menus.sync({ alter: true });

module.exports = Menus;